<template>
  <div class="btnList">
    <div class="btn" :class="index == btnIndex ? 'active_btn' : ''" v-for="btn, index in btnList" :key="index"
      @click="enumClick(btn, index)">{{ btn.label }}</div>
  </div>
</template>

<script>
import energyApi from '@/api/energy'
export default {
  name: 'BtnList',
  props: {
    setBtnIndex: {
      type: Number,
      default: () => {
        return 0
      }
    }
  },
  data() {
    return {
      btnIndex: 0,
      btnList: [],
    };
  },
  mounted() {
    this.btnIndex = this.setBtnIndex;
    this.queryType();
  },
  methods: {
    enumClick(e, index) {
      this.btnIndex = index;
      this.$emit('enumClick', e);
    },
    async queryType() {
      let { data: energyList } = await energyApi.queryByCondition({})
      this.energyList = energyList || [];
      this.btnList = this.energyList.map(e => {
        return {
          label: e.energyName,
          value: e.energyId,
          ...e
        }
      })
      this.btnList.length ? this.$emit('enumClick', this.btnList[0]) : '';
    }
  }
};
</script>

<style lang="scss" scoped>
.btnList {
  width: fit-content;
  display: flex;
  align-items: center;
  border: 1px solid #6176D0;
  border-radius: 5px;
  overflow: hidden;

  .btn {
    padding: 0 8px;
    background-color: #ffffff;
    border-right: 1px solid #6176D0;
    font-size: 14px;
    line-height: 24px;
    color: #6176D0;
    cursor: pointer;
    transition: all 0.2s;

    &:hover {
      background-color: #6175d042;
    }

    &:last-child {
      border: none;
    }
  }

  .active_btn {
    background-color: #6176D0;
    color: #ffffff;

    &:hover {
      background-color: #6176D0;
    }
  }
}
</style>